import json
from datetime import datetime, timedelta

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.safestring import mark_safe
from stdimage import StdImageField


# Create your models here.

class User(AbstractUser):
    gender_choices = (
        (0, '男'),
        (1, '女'),
        (2, '保密'),
    )
    mobile = models.CharField(max_length=25, unique=True, verbose_name='手机号')
    avatar = StdImageField(upload_to='avatar/%Y/', default='avatar/2024/default.png', verbose_name='头像',
                           variations={
                               'thumb_400x400': (400, 400),
                               'thumb_50x50': (50, 50, True),
                           })

    nickname = models.CharField(max_length=32, null=True, blank=True, verbose_name='昵称')
    signature = models.CharField(max_length=256, null=True, blank=True, verbose_name='签名')
    age = models.IntegerField(null=True, blank=True, verbose_name='年龄')
    gender = models.SmallIntegerField(choices=gender_choices, default=2, verbose_name='性别')
    disable_time = models.DateTimeField(default=datetime.now() + timedelta(days=365*100), verbose_name='封禁时间')  # 封禁时间
    is_active = models.BooleanField(default=True, verbose_name='是否激活')  # 可以用来判断用户是否封号
    is_show = models.BooleanField(default=True, verbose_name='是否显示')    # 可以用来判断用户是否隐藏

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name


    def avatar_small(self):
        if self.avatar:
            return mark_safe(f'<img style="border-radius:100%;" src="{self.avatar.thumb_50x50.url}" >')
        return ""

    avatar_small.short_description = "个人头像（50x50）"
    avatar_small.allow_tags = True
    avatar_small.admin_order_field = "avatar"

    def avatar_medium(self):
        if self.avatar:
            return mark_safe(f'<img style="border-radius:100%;" src="{self.avatar.thumb_400x400.url}" >')
        return ""

    avatar_medium.short_description = "个人头像（400x400)"
    avatar_medium.allow_tags = True
    avatar_medium.admin_order_field = "avatar"


